package com.chengzhi.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

/**
 * @description ex-config
 * @author: ruanchengzhi
 * @create: 2020-12-24 10:55
 **/
//@Component
public class EsConfig extends AbstractElasticsearchConfiguration {

    @Autowired
    private ElasticsearchRestClientProperties properties;

    @Bean
    public ElasticsearchRestTemplate restTemplate() {
        return new ElasticsearchRestTemplate(elasticsearchClient());
    }

    @Override
    public RestHighLevelClient elasticsearchClient() {
        ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo(properties.getUris().get(0))
                .withConnectTimeout(properties.getConnectionTimeout())
                .withSocketTimeout(properties.getReadTimeout())
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}
